<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script>
    /**
       * 递归平铺转为一维数组
       * @param {*} treeNode 传进来的值，可以是数组 可以是一个对象
       * @param {*} childKey 树形结构 children
       * @returns
       */
    function newArrFn (arr) {
      return arr.reduce((acc, cur) => {
        if (Array.isArray(cur.children)) {
          // acc = acc.concat(cur)
          // return acc.concat(newArrFn(cur.children))
          return acc.concat(cur, newArrFn(cur.children))
        } else {
          return acc.concat(cur)
        }
      }, [])
    }
    var treeMenu = [
      {
        name: "1",
        id: 1,
        children: [
          {
            name: "1.1",
            id: 1.1,
            children: [
              {
                name: "1.1.1",
                id: 1.11,
                children: null,
              },
            ],
          },
          {
            name: "1.2",
            id: 1.2,
            children: null,
          },
        ],
      },
      {
        name: "2",
        id: 2,
        children: [
          {
            name: "2.1",
            id: 2.1,
            children: null,
          },
          {
            name: "2.2",
            id: 2.2,
            children: null,
          },
        ],
      }
    ];
    console.log(newArrFn(treeMenu))

  </script>
</body>

</html>